﻿using DG.Tweening;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;

namespace UIFrameWork
{
    public class WindowBase : WindowBehaviour
    {
        /// <summary>
        ///  所有Button列表
        /// </summary>
        private List<Button> _allButtonList = new List<Button>();
        /// <summary>
        ///  所有的Toggle列表
        /// </summary>
        private List<Toggle> _allToggleList = new List<Toggle>();
        /// <summary>
        ///  所有的输入框列表
        /// </summary>
        private List<InputField> _allInputFieldList = new List<InputField>();

        private CanvasGroup _uiMask;
        private CanvasGroup _canvasGroup;
        protected Transform _uiContent;
        protected bool _disableAnim = false;//禁用动画

        /// <summary>
        ///  初始化基类组件
        /// </summary>
        private void InitBaseComponent()
        {
            _canvasGroup = transform.GetComponent<CanvasGroup>();
            _uiMask = transform.Find("UIMask").GetComponent<CanvasGroup>();
            _uiContent = transform.Find("UIContent").transform;
        }

        #region 生命周期
        public override void OnAwake()
        {
            base.OnAwake();
            InitBaseComponent();
        }
        public override void OnShow()
        {
            base.OnShow();
            ShowAnimation();
        }
        public override void OnUpdate()
        {
            base.OnUpdate();

        }
        public override void OnHide()
        {
            base.OnHide();

        }
        public override void OnDestroy()
        {
            base.OnDestroy();
            RemoveAllButtonListener();
            RemoveAllToggleListener();
            RemoveAllInputListener();
            _allButtonList.Clear();
            _allToggleList.Clear();
            _allInputFieldList.Clear();
        }
        #endregion

        #region 动画管理
        public void ShowAnimation()
        {
            //基础弹窗不需要动画
            if (canvas.sortingOrder > 90 && _disableAnim == false)
            {
                //Mask动画
                //_uiMask.alpha = 0;
                //_uiMask.DOFade(1, 0.2f);
                //缩放动画
                _uiContent.localScale = Vector3.one * 0.8f;
                _uiContent.DOScale(Vector3.one, 0.3f).SetEase(Ease.OutBack);
            }
        }
        public void HideAnimation()
        {
            if (canvas.sortingOrder > 90 && _disableAnim == false)
            {
                _uiContent.DOScale(Vector3.one * 0.7f, 0.2f).SetEase(Ease.InBack).OnComplete(() =>
                {
                    UIModule.Ins.HideWindow(name);
                });
            }
            else
            {
                UIModule.Ins.HideWindow(name);
            }
        }
        #endregion

        public void ShowWindow()
        {
            UIModule.Ins.ShowWindow(name);
        }

        public void HideWindow()
        {
            //UIModule.Ins.HideWindow(name);
            HideAnimation();
        }

        public override void SetVisible(bool isVisible)
        {
            base.SetVisible(isVisible);
            //gameObject.SetActive(isVisible);//临时
            if (_canvasGroup == null)
                _canvasGroup = transform.GetComponent<CanvasGroup>();
            _canvasGroup.alpha = isVisible ? 1 : 0;
            _canvasGroup.blocksRaycasts = isVisible;
            visible = isVisible;
        }

        public void SetMaskViseble(bool isVisible)
        {
            if (!UISetting.Ins.SINGMASK_SYSTEM)
            {
                return;
            }
            _uiMask.alpha = isVisible ? 1 : 0;
        }

        #region 事件管理

        public void AddButtonClickListener(Button btn, UnityAction action)
        {
            if (btn != null)
            {
                if (!_allButtonList.Contains(btn))
                {
                    _allButtonList.Add(btn);
                }
                btn.onClick.RemoveAllListeners();
                btn.onClick.AddListener(action);

            }
        }
        public void AddToggleClickListener(Toggle toggle, UnityAction<bool, Toggle> action)
        {
            if (toggle != null)
            {
                if (!_allToggleList.Contains(toggle))
                {
                    _allToggleList.Add(toggle);
                }
                toggle.onValueChanged.RemoveAllListeners();
                toggle.onValueChanged.AddListener(isOn =>
                {
                    action?.Invoke(isOn, toggle);
                });
            }
        }
        public void AddInputFieldListener(InputField input, UnityAction<string> onChange, UnityAction<string> endAction)
        {
            if (input != null)
            {
                if (!_allInputFieldList.Contains(input))
                {
                    _allInputFieldList.Add(input);
                }
                input.onValueChanged.RemoveAllListeners();
                input.onEndEdit.RemoveAllListeners();
                input.onValueChanged.AddListener(onChange);
                input.onEndEdit.AddListener(endAction);
            }
        }

        public void RemoveAllButtonListener()
        {
            foreach (var btn in _allButtonList)
            {
                btn.onClick.RemoveAllListeners();
            }
        }

        public void RemoveAllToggleListener()
        {
            foreach (var item in _allToggleList)
            {
                item.onValueChanged.RemoveAllListeners();
            }
        }

        public void RemoveAllInputListener()
        {
            foreach (var item in _allInputFieldList)
            {
                item.onValueChanged.RemoveAllListeners();
                item.onEndEdit.RemoveAllListeners();
            }
        }

        #endregion
    }
}
